<?php
namespace Modules\Backstage\Controllers;

class AccountRolesController extends \Phalcon\Mvc\Controller {
	public function beforeExecuteRoute(\Phalcon\Mvc\Dispatcher $dispatcher) {
		$this -> response -> setStatusCode(200, 'OK') -> sendHeaders() -> setContentType('application/json', 'UTF-8');

		$service = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($service['code'] != 0) {
			$this -> response -> setJsonContent(['code'=>-1,'msg'=>$service['message'] ]) -> send();
			exit;
		}

		$this -> account::getAclInstance() -> check($this, $dispatcher, $service['model'] -> roles_id);
	}

	public function indexAction() {
		$res = $this -> account::getAclInstance() -> getRoles();
		if($res['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$res['code'],'msg'=>$res['message']]);
		}
		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'models' => $res['models']]);
	}

	public function createAction() {
		$res = $this -> account::getAclInstance() -> addRole([
			'name' => $this -> request -> getPost('name'),
			'description' => $this -> request -> getPost('description')
		]);
		if($res['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$res['code'],'msg'=>$res['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'创建成功']);
	}

	public function deleteAction($id) {
		$res = $this -> account::getAclInstance() -> deleteRole($id);
		if($res['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$res['code'],'msg'=>$res['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'删除成功']);
	}

	public function resourcesAction($id) {
		$res = $this -> account::getAclInstance() -> getAccessList($id);
		if($res['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$res['code'],'msg'=>$res['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success','models' => $res['models']]);
	}

	public function updateResourcesAction($id) {
		$put = $this -> request -> getPut();
		$res = $this -> account::getAclInstance() -> addAccessLists([
			'roles_id' => $id,
			'resources_id' => $put['resources_id'],
			'allowed' => $put['allowed']
		]);

		if($res['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$res['code'],'msg'=>$res['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success']);
	}

	public function updateResourcesAccessAction($id, $resources_id) {
		$put = $this -> request -> getPut();
		$res = $this -> account::getAclInstance() -> addAccessList([
			'roles_id' => $id,
			'resources_id' => $resources_id,
			'access_id' => $put['access_id'],
			'allowed' => $put['allowed']
		]);

		if($res['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$res['code'],'msg'=>$res['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success','model' => $res['model']]);
	}
}